#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include "../error.h"
#include "sig.h"

/*--------------------------------------------------------------------------*/

static int oldSecs;
static struct sigaction oldAct;

/*--------------------------------------------------------------------------*/

int
setAlarm(int secs, void (*handler)(int))
{
	struct itimerval tout_val;
	static struct sigaction act;

	tout_val.it_interval.tv_sec = 0;
	tout_val.it_interval.tv_usec = 0;
	tout_val.it_value.tv_sec = 0;
	tout_val.it_value.tv_usec = secs * 200000;

	/* Receive client's disconnect message. With a timeout */
	/* Sets an alarm, the restoring the previous alarm and action */
	act.sa_handler = handler;
	if( sigaction(SIGALRM, &act, &oldAct) == -1 )
		return ERROR_UNEX;
	oldSecs = setitimer(ITIMER_REAL, &tout_val, 0);

	return ERROR_OK;

}

/*--------------------------------------------------------------------------*/

int
unsetAlarm()
{
	/* Restore alarm and action */
	alarm(oldSecs);
	if(sigaction(SIGALRM, &oldAct, NULL) == -1)
		return ERROR_UNEX;

	return ERROR_OK;
}

/*--------------------------------------------------------------------------*/

void
nothing(int noArg)
{
	return;
}

/*--------------------------------------------------------------------------*/


